package com.example.chartview

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View

/**
 * 版权：浩博智能科技有限公司 版权所有
 * @author kevin
 * 创建日期：2021/7/2
 * 描述：chart 的学习项目
 *
 */
class MyChartTestView(context: Context, attrs: AttributeSet?) : View(context, attrs) {


    // 绘制 X 轴 Y 轴
    private var mPaintX: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    //  绘制 X 轴 Y 轴文字
    private var mPaintTextNonius : Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private var rect:Rect ?=null

    private var lineWidth = 2f
    private var maxWid = 0
    private var maxHeight = 0

    private var linePaddingTop = 40f   // XY轴线的居 顶部 间隔
    private var linePaddingStart = 80f   // XY轴线的居 左 间隔
    private var linePaddingEnd = 40f   // 内容的居 右 间隔
    private var linePaddingBottom = 0f   // XY轴线的居 底部 间隔

    private val xTextLength = 7 //x 轴显示的数量
    private val yTextLength = 5 //y 轴显示的数量




    //初始化画笔
    init {
        initPaint()
    }

    private fun initPaint() {

        mPaintX?.apply {
            color = Color.WHITE  //画笔颜色
            strokeWidth = lineWidth    //画笔宽度
            style = Paint.Style.FILL    //画笔的样式，粗体 还是空心STROKE 还是填充 FILL_AND_STROKE
        }

        rect = Rect(100,100,105,200)    //画柱子可以

        mPaintTextNonius.textAlign = Paint.Align.CENTER
        mPaintTextNonius.color =resources.getColor(R.color.textColor)
        mPaintTextNonius.textSize = 30f
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //获取当前View的宽高
        maxWid = w
        maxHeight = h
        //渐变
//        mShader = LinearGradient(mViewWidth.toFloat(), mViewHeight.toFloat(), mViewWidth.toFloat(), 0f, intArrayOf( Color.TRANSPARENT, resources.getColor(R.color.blue)), null, Shader.TileMode.REPEAT)
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
//        canvas!!.draw(rect!!,mPaintX)
        drawLineNonius(canvas!!) //
        drawAxisLine(canvas!!)



    }

    /*
    * 画游标码 xy 轴 （数字/文字） 等
   * */
    private fun drawLineNonius(canvas: Canvas) {
//      先计算每格的宽度
        val gridWid = (maxWid - linePaddingStart -linePaddingEnd)/xTextLength
        var str = "1"
        var tvHei  = getTextHeight(str,mPaintTextNonius)
        linePaddingBottom = tvHei +20f+20f
//      每格子的高度
        val gridHei = (maxHeight - linePaddingBottom- linePaddingTop)/yTextLength
        /*
        *  绘制 X 轴上 （数字/文字）
        * */
        for(i in 0..xTextLength){

            var str = i.toString()
            var tvWid  = getTextWidth(str,mPaintTextNonius)

            canvas.drawText(str,linePaddingStart+(i*gridWid),maxHeight.toFloat() -20f ,mPaintTextNonius)

        }


        /*
        *  绘制 Y 轴上 （数字/文字）
        * */
        for(i in 0..yTextLength){

            var str = i.toString()
            var tvWid  = getTextWidth(str,mPaintTextNonius)
            var tvHei  = getTextHeight(str,mPaintTextNonius)

//             不绘制 0 刻度的文字

            var yTextIndex =linePaddingBottom - (tvHei/2)
                canvas.drawText(str, (linePaddingStart-tvWid)/2, maxHeight - yTextIndex - (i * gridHei) ,mPaintTextNonius)

        }

    }

    /*
    *
    * 绘制 xy 轴线条
    * */
    private val drawAxisLine = { canvas:Canvas ->

        /*
         * 画横线 X轴线
         * */
        canvas!!.drawLine(linePaddingStart,maxHeight-linePaddingBottom,maxWid.toFloat(),maxHeight-linePaddingBottom,mPaintX)
        /*
        * 画竖线 Y轴线
        * */
        canvas!!.drawLine(linePaddingStart,linePaddingTop,linePaddingStart,maxHeight-linePaddingBottom,mPaintX)

    }

    /*
    *  获取文字宽度
    * */
    private fun getTextWidth(text:String, paint:Paint ):Int {
        var rect = Rect() // 文字所在区域的矩形
        paint.getTextBounds(text, 0, text.length, rect)
        return rect.width()
    }

    /*
    *  获取文字高度
    * */
    private fun getTextHeight(text:String, paint:Paint ):Int {
        var rect = Rect() // 文字所在区域的矩形
        paint.getTextBounds(text, 0, text.length, rect)
        return rect.height()
    }

}